package m.jclecai.com.pop;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.base.recyclerview.BaseRecyclerViewAdapter;
import m.jclecai.com.base.recyclerview.BaseViewHolder;
import m.jclecai.com.bean.LotteryNumberMenuBean;

/**
 * Created by andy on 2017/7/7.
 * 这里是快乐十分popupWindow;
 */

public class LotteryNumberPopWindow extends BasePopupWindow {
    private RecyclerView mRvRecyclerView;

    private int mCurrentPosition=0;
    private PopupwindowAdapter mPopupwindowAdapter;
    private GridLayoutManager mPopupLayoutManager;

    public LotteryNumberPopWindow(Context context) {
        super(context);
        init();
    }

    private void init() {
        //这里的话，我们做一个初始化的一个操作；
        setFocusable(true);
        setOutsideTouchable(true);
        setContentView(R.layout.popup_klsf_center);

        mRvRecyclerView = findViewById(R.id.rv_recycler_view);

        mPopupLayoutManager = new GridLayoutManager(getContext(),3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        mRvRecyclerView.setLayoutManager(mPopupLayoutManager);
        mPopupwindowAdapter = new PopupwindowAdapter(null);
        mRvRecyclerView.setAdapter(mPopupwindowAdapter);
    }


    public void setItemData(List<LotteryNumberMenuBean> listData){
        mPopupwindowAdapter.refresh(listData);
    }

    @Override
    protected boolean allowBackgroundTranslucent() {
        return true;
    }

    @Override
    protected int setPopWindowAnimationStyle() {
        return -1;
    }


    /**
     * 显示显示popupWindow里面的内容；
     * @param dropDwonView
     */
    public void show(View dropDwonView){
        showAsDropDown(dropDwonView);
    }


    public class PopupwindowAdapter extends BaseRecyclerViewAdapter<LotteryNumberMenuBean>{

        private int mPostionChecked;

        public PopupwindowAdapter(List<LotteryNumberMenuBean> DList) {
            super(DList);
        }

        @Override
        public int getItemView() {
            return R.layout.template_item1_popup_window;
        }

        @Override
        protected void bindViewHolder(final BaseViewHolder holder, LotteryNumberMenuBean bean, final int position) {

            holder.getTextView(R.id.tv_select_box).setText(bean.getMenuName());
            holder.getTextView(R.id.tv_select_box).setBackgroundResource(R.drawable.select_box_up);
            holder.getTextView(R.id.tv_select_box).setTextColor(getContext().getResources().getColor(R.color.color_666666));

            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //这里我们处理一下点击事件；
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(v, position);
                    }
                    setPostionChecked(position);
                    dismiss();
                }
            });
            if(position == mCurrentPosition){
                holder.getTextView(R.id.tv_select_box).setBackgroundResource(R.drawable.select_box_down);
                holder.getTextView(R.id.tv_select_box).setTextColor(getContext().getResources().getColor(R.color.baseRed));
            }
        }

        public void setPostionChecked(int postionChecked) {

            clearPreSelected();
            mCurrentPosition = postionChecked;

            View view = mPopupLayoutManager.findViewByPosition(postionChecked);
            if(view == null){
                return;
            }
            TextView textView = (TextView) view.findViewById(R.id.tv_select_box);
            textView.setBackgroundResource(R.drawable.select_box_down);
            textView.setTextColor(getContext().getResources().getColor(R.color.baseRed));
        }
    }

    //这里我们是需要取消上一个点击的状态的；
    private void clearPreSelected() {
        View childAt = mPopupLayoutManager.getChildAt(mCurrentPosition);
        if(childAt==null){
            return;
        }

        TextView textView = (TextView) (childAt.findViewById(R.id.tv_select_box));
        textView.setBackgroundResource(R.drawable.select_box_up);
        textView.setTextColor(getContext().getResources().getColor(R.color.color_666666));

    }

    public void setPostionChecked(int postionChecked){
       mPopupwindowAdapter.setPostionChecked(postionChecked);
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int position);
    }

    public OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;
    }
}
